package com.baseproject.imageload;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Pair;

import com.baseproject.BaseApplication;
import com.baseproject.util.DeviceUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hp on 2017/7/18.
 */

public class BitmapBuilder {

    private static Map<Integer, Pair<Integer, Bitmap>> bitmapMap = new HashMap<>();
    public static int BITMAP_TYPE_READER_BACKGROUND = 0;

    int widthPixels = DeviceUtils.getScreenWidth();
    int heightPixels = DeviceUtils.getScreenHeight();
    Bitmap.Config config = Bitmap.Config.ARGB_8888;
    int drawableId = 0;
    int colorId = 0;
    /**
     * 抽象类型，比如背景类型，前景类型
     */
    int type = 0;
    /**
     * 具体类型，比如背景类型中的具体哪个背景或哪个颜色
     */
    int specific = 0;

    public BitmapBuilder(int type, int specific) {
        this.type = type;
        this.specific = specific;
    }

    public BitmapBuilder setWidthPixels(int widthPixels) {
        this.widthPixels = widthPixels;
        return this;
    }

    public BitmapBuilder setHeightPixels(int heightPixels) {
        this.heightPixels = heightPixels;
        return this;
    }

    public BitmapBuilder setConfig(Bitmap.Config config) {
        this.config = config;
        return this;
    }

    public BitmapBuilder setDrawableId(int drawableId) {
        this.drawableId = drawableId;
        return this;
    }

    public BitmapBuilder setColorId(int colorId) {
        this.colorId = colorId;
        return this;
    }

    public Bitmap build() {
        Pair<Integer, Bitmap> savedBitmap = bitmapMap.get(type);
        if (savedBitmap != null && savedBitmap.first == specific) {
            return savedBitmap.second;
        } else if(savedBitmap != null && savedBitmap.first != specific) {
            Bitmap bitmap = savedBitmap.second;
            bitmap = processBitmap(bitmap);
            bitmapMap.put(type, new Pair<>(specific, bitmap));
            return bitmap;
        } else {
                Bitmap bitmap = Bitmap.createBitmap(widthPixels, heightPixels, config);
                bitmap = processBitmap(bitmap);
                bitmapMap.put(type, new Pair<>(specific, bitmap));
                return bitmap;
            }
        }

    private Bitmap processBitmap(Bitmap bitmap) {
        if (drawableId != 0 && colorId == 0) {
//            FutureTarget<Bitmap> futureTarget = Glide.with(BaseApplication.getApplication().getApplicationContext())
//                    .load(drawableId)
//                    .asBitmap()
//                    .into(250, 250);
//            try {
//                bitmap = futureTarget.get();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } catch (ExecutionException e) {
//                e.printStackTrace();
//            }
            // do things with bitmap and then release when finished:
//            Glide.clear(futureTarget);

            bitmap = BitmapFactory.decodeResource(BaseApplication.getApplication().getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
        } else if (colorId != 0 && drawableId == 0) {
            bitmap.eraseColor(BaseApplication.getApplication().getResources().getColor(colorId));
        } else {
            throw new RuntimeException("you prefer to supply drawableId or colorId to construct bitmap");
        }
        return bitmap;
    }

}

